package com.boot.security.server.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.boot.security.server.dao.AppFamMembeDao;
import com.boot.security.server.hitapp.controller.WeixinController;
import com.boot.security.server.model.AppFamMembe;
import com.boot.security.server.utils.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.boot.security.server.page.table.PageTableRequest;
import com.boot.security.server.page.table.PageTableHandler;
import com.boot.security.server.page.table.PageTableResponse;
import com.boot.security.server.page.table.PageTableHandler.CountHandler;
import com.boot.security.server.page.table.PageTableHandler.ListHandler;
import com.boot.security.server.dao.AppAccountDao;
import com.boot.security.server.model.AppAccount;

import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/appAccounts")
public class AppAccountController {
    private static Logger log = LoggerFactory.getLogger(WeixinController.class);
    @Autowired
    private AppAccountDao appAccountDao;

    @Autowired
    private AppFamMembeDao appFamMembeDao;

    @PostMapping
    @ApiOperation(value = "保存")
    public AppAccount save(@RequestBody AppAccount appAccount) {
        appAccountDao.save(appAccount);

        return appAccount;
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id获取")
    public AppAccount get(@PathVariable Long id) {
        return appAccountDao.getById(id);
    }

    @PutMapping
    @ApiOperation(value = "修改")
    public AppAccount update(@RequestBody AppAccount appAccount) {
        appAccountDao.update(appAccount);

        return appAccount;
    }

    @GetMapping
    @ApiOperation(value = "列表")
    public PageTableResponse list(PageTableRequest request) {
        return new PageTableHandler(new CountHandler() {

            @Override
            public int count(PageTableRequest request) {
                return appAccountDao.count(request.getParams());
            }
        }, new ListHandler() {

            @Override
            public List<AppAccount> list(PageTableRequest request) {
                return appAccountDao.list(request.getParams(), request.getOffset(), request.getLimit());
            }
        }).handle(request);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    public void delete(@PathVariable Long id) {
        appAccountDao.delete(id);
    }


    /**
     * 新增
     *
     * @param appAccount
     * @return R
     */
    @PostMapping("/add")
    public R addinfo(@RequestBody AppAccount appAccount) {

        AppFamMembe appFamMembe = new AppFamMembe();
        appFamMembe.setCsrq(appAccount.getCsrq());
        appFamMembe.setPhoneNo(appAccount.getPhoneNo());
        appFamMembe.setRealName(appAccount.getRealName());
        appFamMembeDao.save(appFamMembe);
        AppAccount u = appAccountDao.getById(appAccount.getId());
        if (u != null) {
            return new R<>(appAccountDao.update(appAccount));
        }
        return new R<>(appAccountDao.save(appAccount));
    }

    /**
     * 修改
     *
     * @param appAccount
     * @return R
     */
    @PostMapping("/upd")
    public R updinfo(@RequestBody AppAccount appAccount) {

        return new R<>(appAccountDao.update(appAccount));
    }

    /**
     * 删除
     *
     * @param appAccount
     * @return R
     */
    @PostMapping("/del")
    public R delinfo(@RequestBody AppAccount appAccount) {


        return new R<>(appAccountDao.delete(appAccount.getId()));
    }

    /**
     * 查询
     *
     * @param appAccount
     * @return R
     */
    @PostMapping("/find")
    @ApiOperation(value = "查询卡号绑定的患者信息")
    public R findinfo(@RequestBody AppAccount appAccount) {
        log.info("openid-----------------" + appAccount.getOpenId());
        AppAccount getBylist = appAccountDao.getBylist(appAccount);
        if (getBylist == null) {
            return new R("", "", 100);
        }
        return new R(getBylist);
    }

    /**
     * 查询绑定关系
     * @param params
     * @return R
     */
//    @PostMapping("/find")
//    @ResponseBody
//    public R findinfo(@RequestParam Map<String, String> params){
//
//      //  Map<String,Object> map = new HashMap<String,Object  >();
////        String timestarm=params.get("timestarm").toString();
////        //    map=entityToMap(appAccount);
////        AppAccount appAccount =new AppAccount();
//        return new R<>( );
//
//        //    return new R<>(  bxbFbqksDao.getById(bxbFbqks.getId()));
//    }
}
